package controllers;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Point2D;

import javax.swing.JPanel;

import util.util.DDict;

public class MyController implements
	ComponentListener, 
	MouseListener, 
	MouseMotionListener, 
	MouseWheelListener
{

	protected JPanel	panel;
	
	protected DDict<Integer,Boolean>		mPressed		= new DDict<Integer,Boolean>(false);
	protected DDict<Integer,Point2D>		mScreenDown		= new DDict<Integer,Point2D>(new Point(0,0)); 
	protected DDict<Integer,Point2D>		mScreenUp		= new DDict<Integer,Point2D>(new Point(0,0)); 
	protected Point2D						mScreenPos		= new Point(0,0);
	private boolean							debug			= false;
	
	public MyController(JPanel P) {
		println("MyPanel");
//		setPanel(panel);
	}
	
	public void println(String S) {
		if (this.isDebug()) {
			System.out.println(S);
		}
	}
	
	public void refresh() {
		println("refresh");
		panel.paintImmediately(panel.getVisibleRect());
	}
	
	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		println("mouseWheelMoved");
		mScreenPos = e.getPoint();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		println("mouseDragged");
		mScreenPos = e.getPoint();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		println("mouseMoved");
		mScreenPos = e.getPoint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		println("mouseClicked");
		mScreenPos = e.getPoint();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		println("mousePressed");
		mScreenPos = e.getPoint();
		mScreenDown.put(e.getButton(), mScreenPos);
		mPressed.put(e.getButton(), true);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		println("mouseReleased");
		mScreenPos = e.getPoint();
		mScreenUp.put(e.getButton(), mScreenPos);
		mPressed.put(e.getButton(), false);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		println("mouseEntered");
		mScreenPos = e.getPoint();
	}

	@Override
	public void mouseExited(MouseEvent e) {
		println("mouseExited");
		mScreenPos = e.getPoint();		
	}

	@Override
	public void componentResized(ComponentEvent e) {
		println("componentResized");
		refresh();		
	}

	@Override
	public void componentMoved(ComponentEvent e) {
		println("componentMoved");
		refresh();
	}

	@Override
	public void componentShown(ComponentEvent e) {
		println("componentShown");
		refresh();		
	}

	@Override
	public void componentHidden(ComponentEvent e) {
		println("componentHidden");
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean myPanelDebug) {
		this.debug = myPanelDebug;
	}

	public JPanel getPanel() {
		return panel;
	}

	public void setPanel(JPanel panel) {
		if (panel!=null) {
			panel.removeComponentListener(this);
			panel.removeMouseListener(this);
			panel.removeMouseWheelListener(this);
			panel.removeMouseMotionListener(this);
		}
		this.panel = panel;
		
		panel.addComponentListener(this);
		panel.addMouseListener(this);
		panel.addMouseMotionListener(this);
		panel.addMouseWheelListener(this);
	}

	public void paint(Graphics G) {
	}
		
}
